package com.augurit.gis.common.util;

import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Random;
import java.util.regex.Matcher;

public class DateUtil {

	public static String date2SqlString(java.util.Date date,boolean wholeFlag){
		if(date == null)return "null";
		String str = "yyyy-MM-dd";
		if(wholeFlag){
			str="yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat(str);
		return "to_date('"+dateFormat.format(date)+"','"+str+"')";
	}
	public static String getCurChinaString(){
		Date date = new Date(System.currentTimeMillis());
		DateFormat format = DateFormat.getDateInstance(DateFormat.LONG, Locale.CHINA);
		return format.format(date);
	}
	public static String date2yyyymm(Date date){
		return date2ChinaString(date,2);
	}
	public static String date2ChinaString(java.util.Date date){
		return date2ChinaString(date,3);
	}
	public static String formatDate(java.util.Date date, String regex){
		if(date==null){
			return ""; 
		}
		String str = null;
		try {
			if(!FormUtil.isEmpty(regex)){
				SimpleDateFormat dateFormat = new SimpleDateFormat(regex);
				str = dateFormat.format(date);
			}else{
				str = date2String(date);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			str = date2String(date);
		}
		return str;
	}	
	
	/**
	 * 
	 * @param date
	 * @param flag
	 * @return
	 */
	public static String date2ChinaString(java.util.Date date,int flag){
		if(date==null){
			return ""; 
		}
		SimpleDateFormat format=null;
		if(flag==1){
			format = new SimpleDateFormat("yyyy年");        
		}else if(flag ==2){
			format = new SimpleDateFormat("yyyy年MM月");
		}else if(flag ==3){
			format = new SimpleDateFormat("yyyy年MM月dd日");
		}else if(flag ==4){
			format = new SimpleDateFormat("yyyy年MM月dd日 HH时");
		}else if(flag ==5){
			format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
		}else if(flag ==6){
			format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
		}else{
			format = new SimpleDateFormat("yyyy-MM-dd");
		}
        return format.format(date);
	}
	public static boolean checkDateString(String datestr){
		String patternstr = "\\s*([0-9]{4})\\-([0-9]{1,2})\\-([0-9]{1,2}\\s*)";
		java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(patternstr);
		Matcher matcher = pattern.matcher(datestr);
		boolean isDateType = matcher.find();
		if(isDateType){
			String[] dateArray = datestr.split("-");
			try{
				int year = Integer.parseInt(dateArray[0]);
				int month = Integer.parseInt(dateArray[1]);
				int date = Integer.parseInt(dateArray[2]);
				if(month>12||month==0)
					isDateType = false;
				if(date>31||date==0)
					isDateType = false;
				if(month==2){
					if(year%4==0&&date>29)
						isDateType = false;
					if(year%4!=0&&date>28)
						isDateType = false;
				}else if(month==4||month==6||month==9||month==11){
					if(date>30)isDateType = false;
				}
			}catch (Exception e) {
				isDateType = false;
			}
			
		}
		return isDateType;
	}
	public static String date2String(java.util.Date date){
		if(date==null){
			return null;
		}else{
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			String str = format.format(date); 
			return str;
		}
	}
	

	
	/**
	 * 生成一个年份小于等于maxYear大于等于minYear的日期对象
	 * @param maxYear
	 * @param minYear
	 * @return
	 */
	private static Date getDate(int year){
		return getDate(year,year);
	}
	private static Date getDate(int maxYear,int minYear){
		Random random = new Random();
		int month = random.nextInt(12)+1;
		int day = 0 ;
		if(month==1||month==3||month==4||month==7||month==8||month==10||month==12){
			day = random.nextInt(31)+1;
		}else{
			if(month ==2){
				day = random.nextInt(28)+1;
			}else{
				day = random.nextInt(30)+1;
			}
		}
		int tempYear = 0;
		if(maxYear<minYear){
			tempYear = maxYear;
			maxYear = minYear;
			minYear = tempYear;
		}
		
			int year = random.nextInt(maxYear-minYear+1)+minYear;
			Date date = new Date(year-1900,month-1,day);
			return date;
		}
	public static String getDateStringForSQL(int year){
		return getDateStringForSQL(year,year);
	}
	public static String getDateStringForSQL(int maxYear,int minYear){
		Date date = getDate(maxYear,minYear);
		String dateSQL = "to_date('"+date+"','yyyy-mm-dd')";
		return dateSQL;
	}
	public static String getDateStringForSQL(Date date){
		String dateSQL = "to_date('"+date+"','yyyy-mm-dd')";
		return dateSQL;
	}
	public static String addDateAndGetStringSQL(Date date,int dayCount){
		return null;
	}
	public static Date addDate(Date date,int dayCount){
		Date addDate = new Date(date.getYear(),date.getMonth(),date.getDate()+dayCount);
		return addDate;
	}
	public static void main(String[] args) {
//		Date date = new Date(System.currentTimeMillis());
		java.util.Date date = new java.util.Date();
		System.out.println(date2ChinaString(date));
//		test2();
		
	}

}
